
package com.kgc.sbt.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.CartApi;
import com.kgc.sbt.domain.*;
import com.kgc.sbt.mapper.CartMapper;
import com.kgc.sbt.mapper.GoodsFromMapper;
import com.kgc.sbt.mapper.UserFromMapper;
import com.kgc.sbt.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.List;

/**
 * Created on 2021/6/22.
 * <p>
 * Author: a123
 * <p>
 * Description:
 */
@Service
public class CartProvider implements CartApi {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private GoodsFromMapper goodsFromMapper;

    @Autowired
    private UserFromMapper userFromMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public List<Cart> selectById(Integer userId) {

        // 接收参数和持久化参数对象不一致，需要转换
        CartExample cartExample = new CartExample();
        CartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);

        return cartMapper.selectByExample(cartExample);
    }

    @Override
    public Goods selectGoodsById(Integer id) {

        return goodsFromMapper.selectByPrimaryKey(id);
    }

    @Override
    public User selectUserById(Integer id) {

        return userFromMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean insertCart(Integer userId,Integer goodsId,Integer number) {

        Cart cart = new Cart();
        cart.setCreateTime(new Date());
        cart.setGoodsId(goodsId);
        cart.setNumber(number);
        cart.setUserId(userId);
        cart.setIsExists(1);

        return cartMapper.insert(cart) > 0;
    }

    @Override
    public boolean isTradeOrderCreated(String tradeOrderNo) {
        return ObjectUtils.isEmpty(redisUtils.get(tradeOrderNo));
    }

    @Override
    public List<Cart> selectCartByIds(Integer[] ids) {
        return cartMapper.selectByIds(ids);
    }

    @Override
    public boolean delCartByIds(Integer[] ids) {
        return cartMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<CartFrom> selectCartByUserId(Integer userId) {


        return cartMapper.selcetCartByUserId(userId);
    }

    @Override
    public boolean updateCartNumberAdd(Integer userId, Integer goodsId, Integer numberAdd) {

        return cartMapper.updateCaryNumberAdd(userId,goodsId,numberAdd)>0;
    }

    @Override
    public boolean updateCartNumberDel(Integer userId, Integer goodsId, Integer numberDel) {

        return cartMapper.updateCaryNumberDel(userId,goodsId,numberDel)>0;
    }

    @Override
    public Cart selectCartByUserIdAndGoodsId(Integer userId, Integer goodsId) {

        return cartMapper.selectCartByUserIdAddGoodsId(userId,goodsId);
    }

}

